package com.neusoft.elm.dao.impl;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {

     Connection conn=null;
     PreparedStatement prep=null;
     ResultSet res=null;
    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws Exception{
        try {
            List<Business>businessList=new ArrayList<>();
            Business business=new Business();
            conn=DBUtil.getConnection();
            String sql="select * from business where orderTypeId=?";
            prep=conn.prepareStatement(sql);
            prep.setInt(1,orderTypeId);
            res=prep.executeQuery();
            while (res.next()){
                business.setBusinessAddress(res.getString("businessAddress"));
                business.setBusinessExplain(res.getString("businessExplain"));
                business.setBusinessId(res.getInt("businessId"));
                business.setBusinessImg(res.getString("businessImg"));
                business.setBusinessName(res.getString("businessName"));
                business.setRemarks(res.getString("remarks"));
                business.setDeliveryPrice(res.getDouble("deliveryPrice"));
                business.setStarPrice(res.getDouble("starPrice"));


businessList.add(business);


            }
            return businessList;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(res,prep);
        }


    }
    @Override
    public Business getBusinessById(Integer businessId) throws Exception{

        try {
            String sql=" select * from business where businessId=?";
            Business business = null;
            conn=DBUtil.getConnection();

            prep=conn.prepareStatement(sql);
            prep.setInt(1,businessId);
            res=prep.executeQuery();

            while (res.next()){
business=new Business();
                business.setBusinessId(res.getInt("businessId"));
                business.setBusinessName(res.getString("businessName"));
                business.setBusinessAddress(res.getString("businessAddress"));
                business.setBusinessExplain(res.getString("businessExplain"));
                business.setBusinessImg(res.getString("businessImg"));
                business.setOrderTypeId(res.getInt("orderTypeId"));
                business.setStarPrice(res.getDouble("starPrice"));
                business.setDeliveryPrice(res.getDouble("deliveryPrice"));
                business.setRemarks(res.getString("remarks"));
            }
            return  business;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(res,prep);
        }


    }

}
